﻿using GalaSoft.MvvmLight.Messaging;
using KINLO.DRV.Drv;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Modbus;
using Modbus.Device;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using log4net.Repository.Hierarchy;

namespace KINLO.DRV.Machine
{
    public class IVInjMachine
    {
        #region 字段

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(IVInjMachine));
        public ModbusTcp PLC;
        int NNetId;
        public System.Timers.Timer timer;
        public string Name;
        public string IP;
        public int Port;
        object locker = new object();

        Dictionary<TaskTypeEnum, Task> dictTasks = new Dictionary<TaskTypeEnum, Task>();
        #endregion

        #region 属性 PLC TO PC 

        Object FWeightClearLock = new object();
        ushort mw300;
        /// <summary>
        /// 前称重清零
        /// </summary>
        public ushort MW300
        {
            get { return mw300; }
            set
            {
                mw300 = value;
                if (value == 1 && (!dictTasks.ContainsKey(TaskTypeEnum.前称重清零)))
                {
                    var task = Task.Factory.StartNew((S) =>
                    {
                        var msg = string.Format("启动前称重清零 值:{0} ", MW300);
                        Messenger.Default.Send(msg, "ReceiveEventInfo");
                        log.Info(msg);
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        Messenger.Default.Send("", "FWeightClear");
                        stopwatch.Stop();
                        var msg2 = $"前称重清零完成 {stopwatch.ElapsedMilliseconds}";
                        Messenger.Default.Send(msg2, "ReceiveEventInfo");
                        log.Info(msg2);
                    }, "", TaskCreationOptions.LongRunning).ContinueWith(t => dictTasks.Remove(TaskTypeEnum.前称重清零));
                    dictTasks.Add(TaskTypeEnum.前称重清零, task);
                }
            }
        }

        Object FInjWeightLock = new object();
        ushort mw301;
        /// <summary>
        /// 前称重命令
        /// </summary> 
        public ushort MW301
        {
            get { return mw301; }
            set
            {
                mw301 = value;
                if (value == 1 && (!dictTasks.ContainsKey(TaskTypeEnum.前称重)))
                {
                    var task = Task.Factory.StartNew((S) =>
                    {

                        var msg = string.Format("启动前称重命令 值:{0} ", MW301);
                        Messenger.Default.Send(msg, "ReceiveEventInfo");
                        log.Info(msg);
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        Messenger.Default.Send("", "FInjWeight");
                        stopwatch.Stop();
                        var msg2 = $"前称重完成 {stopwatch.ElapsedMilliseconds}";
                        Messenger.Default.Send(msg2, "ReceiveEventInfo");
                        log.Info(msg2);
                    }, "", TaskCreationOptions.LongRunning).ContinueWith(t => dictTasks.Remove(TaskTypeEnum.前称重));
                    dictTasks.Add(TaskTypeEnum.前称重, task);
                }
            }
        }

        Object BWeightClearLock = new object();
        ushort mw302;
        /// <summary>
        /// 后称重清零
        /// </summary>
        public ushort MW302
        {
            get { return mw302; }
            set
            {
                mw302 = value;
                if (value == 1 && (!dictTasks.ContainsKey(TaskTypeEnum.后称重清零)))
                {
                    var task = Task.Factory.StartNew((S) =>
                    {

                        var msg = string.Format("启动后称重清零 值:{0} ", MW302);
                        Messenger.Default.Send(msg, "ReceiveEventInfo");
                        log.Info(msg);
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        Messenger.Default.Send("", "BWeightClear");
                        stopwatch.Stop();
                        var msg2 = $"后称重清零完成 {stopwatch.ElapsedMilliseconds}";
                        Messenger.Default.Send(msg2, "ReceiveEventInfo");
                        log.Info(msg2);
                    }, "", TaskCreationOptions.LongRunning).ContinueWith(t => dictTasks.Remove(TaskTypeEnum.后称重清零));
                    dictTasks.Add(TaskTypeEnum.后称重清零, task);
                }

            }
        }

        Object BInjWeightLock = new object();
        ushort mw303;
        /// <summary>
        /// 后称重命令
        /// </summary> 
        public ushort MW303
        {
            get { return mw303; }
            set
            {

                mw303 = value;
                if (value == 1 && (!dictTasks.ContainsKey(TaskTypeEnum.后称重)))
                {
                    var task = Task.Factory.StartNew((S) =>
                    {

                        var msg = string.Format("启动后称重 值:{0} ", MW303);
                        Messenger.Default.Send(msg, "ReceiveEventInfo");
                        log.Info(msg);
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        Messenger.Default.Send("", "BInjWeight");
                        stopwatch.Stop();
                        var msg2 = $"后称重完成 {stopwatch.ElapsedMilliseconds}";
                        Messenger.Default.Send(msg2, "ReceiveEventInfo");
                        log.Info(msg2);
                    }, "", TaskCreationOptions.LongRunning).ContinueWith(t => dictTasks.Remove(TaskTypeEnum.后称重));
                    dictTasks.Add(TaskTypeEnum.后称重, task);

                }
            }
        }

        ushort mw304;
        /// <summary>
        /// 前称重托架号
        /// </summary> 
        public ushort MW304
        {
            get { return mw304; }
            set
            {
                if (mw304 != value)
                {
                    mw304 = value;
                    var msg = string.Format("收到 前称重托架号 值:{0} ", MW304);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);
                }
            }
        }

        ushort mw305;
        /// <summary>
        /// 前称重托架行号
        /// </summary> 
        public ushort MW305
        {
            get { return mw305; }
            set
            {
                if (mw305 != value)
                {
                    mw305 = value;
                    var msg = string.Format("收到 前称重托架行号 值:{0} ", MW305);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);
                }
            }
        }

        ushort mw306;
        /// <summary>
        /// 后称重托架号
        /// </summary> 
        public ushort MW306
        {
            get { return mw306; }
            set
            {
                if (mw306 != value)
                {
                    mw306 = value;
                    var msg = string.Format("收到 后称重托架号 值:{0} ", MW306);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);
                }
            }
        }

        ushort mw307;
        /// <summary>
        /// 后称重托架行号
        /// </summary> 
        public ushort MW307
        {
            get { return mw307; }
            set
            {
                if (mw307 != value)
                {
                    mw307 = value;
                    var msg = string.Format("收到 后称重托架行号 值:{0} ", MW307);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);

                }
            }
        }
        ushort mw299;
        /// <summary>
        /// 注液站号
        /// </summary> 
        public ushort MW230
        {
            get { return mw299; }
            set
            {
                if (mw299 != value)
                {
                    mw299 = value;
                    var msg = string.Format("收到 注液站号 值:{0} ", MW230);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);

                }
            }
        }
        ushort mw231;
        /// <summary>
        /// 注液站号
        /// </summary> 
        public ushort MW231
        {
            get { return mw231; }
            set
            {
                if (mw231 != value)
                {
                    mw231 = value;
                    var msg = string.Format("收到 注液站层号 值:{0} ", MW231);
                    Messenger.Default.Send(msg, "ReceiveEventInfo");
                    log.Info(msg);
                }
            }
        }


        #endregion

        #region 方法

        public IVInjMachine(string ip, int port, int interval, string name, int nNetId)
        {
            IP = ip;
            Port = port;
            Name = name;
            NNetId = nNetId;
            timer = new System.Timers.Timer(interval);
            timer.Elapsed += Timer_Elapsed;
        }
        public bool IsConnected
        {
            get
            {
                try
                {
                    Ping p = new Ping();
                    var relay = p.Send(IP);
                    if (relay.Status == IPStatus.Success && PLC.IsConnected)
                        return true;
                    else
                        return false;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        int inTimer = 0;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (Interlocked.Exchange(ref inTimer, 1) == 0)
                {
                    if (PLC == null)
                    {
                        PLC = ModbusTcp.CreateIP(1, IP);
                        // PLC = ModbusTcp.CreateIP(new TcpClient(IP, Port));
                        // PLC.Transport.ReadTimeout = 1000;
                        // PLC.Transport.WriteTimeout = 1000;
                    }
                    try
                    {
                        ushort nStartAddr = 30300;
                        ushort nCount = 9;
                        var Values = PLC.ReadHoldingRegisters(nStartAddr, nCount);
                        if (Values != null && Values.Length == 9)
                        {
                            MW304 = Values[4];
                            MW305 = Values[5];
                            MW306 = Values[6];
                            MW307 = Values[7];
                            MW230 = Values[8];

                            MW300 = Values[0];
                            MW301 = Values[1];
                            MW302 = Values[2];
                            MW303 = Values[3];
                        }
                    }
                    catch (Exception ex)
                    {
                        Messenger.Default.Send(string.Format("读保持寄存器失败:{0}", ex.Message), "ReceiveEventInfo");
                        log.Error(string.Format("读保持寄存器失败:{0}", ex.Message));
                    }
                }
                else
                {
                    var errorMs = string.Format("注液机:{0}:{1} 已离线", IP, Port);
                    //log.Error(errorMs);
                    //Messenger.Default.Send(errorMs, "ReceiveEventInfo");
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                Interlocked.Exchange(ref inTimer, 0);
            }
        }

        /// <summary>
        /// 读压力杠数据
        /// </summary>
        public double[] ReadPressureChart()
        {
            try
            {
                if (PLC == null)
                {
                    PLC = ModbusTcp.CreateIP(1, IP);
                }
                try
                {
                    ushort nStartAddr = 30310;
                    ushort nCount = 8;
                    var Values = PLC.ReadPressureCharFloat(nStartAddr, nCount);
                    if (Values != null && Values.Length >=4)
                    {
                        return Values;
                    }
                }
                catch (Exception ex)
                {
                    Messenger.Default.Send(string.Format("读加压杠数据保持寄存器失败:{0}", ex.Message), "ReceiveEventInfo");
                    log.Error(string.Format("读加压杠数据保持寄存器失败:{0}", ex.Message));
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }


        /// <summary>
        /// 注液前称重完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteFInjectionWeightFin()
        {
            try
            {
                var r = PLC.WriteSingleRegister(30405, 1);
                var msg = $"写 注液前称重完成{(r ? "成功" : "失败")}  ";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                var errMsg = string.Format("写 注液前称重完成 异常:{0}", ex.Message);
                log.Error(errMsg);
                Messenger.Default.Send(errMsg, "ReceiveEventInfo");
            }
        }

        /// <summary>
        /// 注液后称重完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBInjectionWeightFin()
        {
            try
            {
                var r = PLC.WriteSingleRegister(30406, 1);
                var msg = string.Format("写 注液后称重完成 成功");
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                log.Error(string.Format("写 注液后称重完成 异常:{0}", ex.Message));
                Messenger.Default.Send(string.Format("写 注液后称重完成 异常:{0}", ex.Message), "ReceiveEventInfo");
            }
        }

        /// <summary>
        ///  清后称重命令
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBInjectionWeightClear()
        {
            try
            {
                PLC.WriteSingleRegister(30303, 0);
            }
            catch { }
        }
        /// <summary>
        ///  清前称重命令
        /// </summary>
        /// <param name="s"></param> 
        public void WriteFInjectionWeightClear()
        {
            try
            {
                PLC.WriteSingleRegister(30301, 0);
            }
            catch { }
        }
        /// <summary>
        /// 清前称清零信号
        /// </summary>
        public void WriteFWeightClear()
        {
            try
            {
                PLC.WriteSingleRegister(30300, 2);
            }
            catch { }
        }
        /// <summary>
        /// 前称清零完成
        /// </summary>
        public void WriteFWeightFin()
        {
            try
            {
                PLC.WriteSingleRegister(30407, 1);
            }
            catch { }
        }
        /// <summary>
        /// 清后称清零信号
        /// </summary>
        public void WriteBWeightClear()
        {
            try
            {
                PLC.WriteSingleRegister(30302, 2);
            }
            catch { }
        }

        /// <summary>
        /// 后称清零完成
        /// </summary>
        public void WriteBWeightFin()
        {
            try
            {
                PLC.WriteSingleRegister(30408, 1);
            }
            catch { }
        }
        /// <summary>
        /// 后称补液量值
        /// </summary>
        /// <param name="index">地址</param>
        /// <param name="result">补液量*10</param>
        public void WriteBInjValue(int index, ushort result)
        {
            try
            {
                var ind = ConvertToAddr(index);
                var r = PLC.WriteSingleRegister(ind, result);
                var msg = string.Format("写 后称补液量值:{0} {1} {2} {3}", ind, index + 1, result, r ? "成功" : "失败");
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                log.Error(string.Format("写 后称补液量值完成 异常:{0}", ex.Message));
                Messenger.Default.Send(string.Format("写 后称补液量值完成 异常:{0}", ex.Message), "ReceiveEventInfo");
            }
        }
        /// <summary>
        /// 前称重结果 
        /// </summary>
        /// <param name="results"></param>
        public void WriteFInjWeightOKNGs(ushort[] results)
        {
            try
            {
                var r = PLC.WriteMultipleRegisters(30409, results);
                var msg = $"写 前称重OKNG结果{(r ? "成功" : "失败")}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                var msg = $"写 前称重OKNG结果异常:{ex.Message}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
        }
        public void WriteBInjWeightRepairValues(ushort[] results)
        {
            try
            {
                var r = PLC.WriteMultipleRegisters(30473, results);
                var msg = $"写 补液量结果{(r ? "成功" : "失败")}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                var msg = $"写 后称重OKNG结果异常:{ex.Message}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
        }
        /// <summary>
        /// 后称重结果
        /// </summary>
        /// <param name="results"></param>
        public void WriteBInjWeightOKNGs(ushort[] results)
        {
            try
            {
                var r = PLC.WriteMultipleRegisters(30441, results);
                var msg = $"写 后称重OKNG结果{(r ? "成功" : "失败")}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                var msg = $"写 后称重OKNG结果异常:{ex.Message}: {string.Join(";", results)}";
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
        }


        /// <summary>
        /// 注液后称重结果
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBInjWeightOKNG(int index, ushort result)
        {
            try
            {
                var ind = ConvertToAddr(index);
                var r = PLC.WriteSingleRegister(ind, result);
                var msg = string.Format("写 后称重OKNG结果:{0} 列{1} 结果{2} {3}", ind, index + 1, result, r ? "成功" : "失败");
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
                log.Info(string.Format("写 注液后称重完成 成功"));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("写 注液后称重完成 异常:{0}", ex.Message));
                Messenger.Default.Send(string.Format("写 注液后称重完成 异常:{0}", ex.Message), "ReceiveEventInfo");
            }
        }

        /// <summary>
        /// 回写是否有电池  前称结果
        /// </summary>
        /// <param name="s"></param> 
        public void WriteHaveBattery(int index, ushort result, string traycode = "", int row = 0)
        {
            try
            {
                var ind = ConvertToAddr1(index);
                var r = PLC.WriteSingleRegister(ind, result);
                var msg = string.Format("写 回写是否有电池 {0} {1} 完成 托架 {2} 行{3} 列 {4} {5}", ind, result, traycode, index < 16 ? row : row + 2, index % 16 + 1, r ? "成功" : "失败");
                Messenger.Default.Send(msg, "ReceiveEventInfo");
                log.Info(msg);
            }
            catch (Exception ex)
            {
                log.Error(string.Format("写 回写是否有电池 {0} {1} 失败:{2}", index, result, ex.Message));
                Messenger.Default.Send(string.Format("写 回写是否有电池 {0} {1} 失败:{2}", index % 16, result, ex.Message), "ReceiveEventInfo");
            }
        }

       // PC侧漏1OK AT %MB61010 :INT ; //0NG  1OK
       public UInt16[] GetLeakHuntingResult()
        {
            try
            {
                var Values = PLC.ReadHoldingRegisters(30505, 32);
                return Values;
            }
            catch (Exception ex)
            {
                return Enumerable.Repeat<UInt16>(2, 32).ToArray();
            }
        }
        /// <summary>
        /// 给PLC在线信号
        /// </summary>
        public void WriteKeepActive()
        {
            try
            {
                PLC.WriteSingleRegister(30400, (ushort)1);
                // log.Info(string.Format("写 给PLC在线信号 "));
            }
            catch (Exception ex)
            {
                Messenger.Default.Send(string.Format("写 给PLC在线信号 失败:{0}", ex.Message), "ReceiveEventInfo");
                log.Info(string.Format("写 给PLC在线信号 失败:{0}", ex.Message));
            }

        }

        /// <summary>
        /// 前电子称在线信号
        /// </summary>
        public void WriteFrontWeightOnline(ushort weightBits)
        {
            try
            {
                PLC.WriteSingleRegister(30401, weightBits);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send(string.Format("写 前电子称在线信号 失败:{0} {1}", ex.Message, weightBits), "ReceiveEventInfo");
                log.Error(string.Format("写 前电子称在线信号 失败:{0} {1}", ex.Message, weightBits));
            }
        }

        public void WriteFrontWeightOnline(ushort[] weightBits)
        {
            try
            {
                PLC.WriteMultipleRegisters(30401, weightBits);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send(string.Format("写 前电子称在线信号 失败:{0} {1}", ex.Message, weightBits), "ReceiveEventInfo");
                log.Error(string.Format("写 前电子称在线信号 失败:{0} {1}", ex.Message, weightBits));
            }
        }

        /// <summary>
        /// 后电子称在线信号
        /// </summary>
        public void WriteBackWeightOnline(ushort weightBits)
        {
            try
            {
                PLC.WriteSingleRegister(30403, weightBits);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send(string.Format("写 后电子称在线信号 失败:{0} {1}", ex.Message, weightBits), "ReceiveEventInfo");
                log.Error(string.Format("写 后电子称在线信号 失败:{0} {1}", ex.Message, weightBits));
            }
        }

        public void WriteBackWeightOnline(ushort[] weightBits)
        {
            try
            {
                PLC.WriteMultipleRegisters(30403, weightBits);
            }
            catch (Exception ex)
            {
                Messenger.Default.Send(string.Format("写 后电子称在线信号 失败:{0} {1}", ex.Message, weightBits), "ReceiveEventInfo");
                log.Error(string.Format("写 后电子称在线信号 失败:{0} {1}", ex.Message, weightBits));
            }
        }
        /// <summary>
        /// 后称重结果地址
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        ushort ConvertToAddr(int index)
        {
            var addrs = new List<ushort>();
            addrs.Add(30441);
            addrs.Add(30442);
            addrs.Add(30443);
            addrs.Add(30444);
            addrs.Add(30445);
            addrs.Add(30446);
            addrs.Add(30447);
            addrs.Add(30448);
            addrs.Add(30449);
            addrs.Add(30450);
            addrs.Add(30451);
            addrs.Add(30452);
            addrs.Add(30453);
            addrs.Add(30454);
            addrs.Add(30455);
            addrs.Add(30456);
            addrs.Add(30457);
            addrs.Add(30458);
            addrs.Add(30459);
            addrs.Add(30460);
            addrs.Add(30461);
            addrs.Add(30462);
            addrs.Add(30463);
            addrs.Add(30464);
            addrs.Add(30465);
            addrs.Add(30466);
            addrs.Add(30467);
            addrs.Add(30468);
            addrs.Add(30469);
            addrs.Add(30470);
            addrs.Add(30471);
            addrs.Add(30472);

            if (index > addrs.Count - 1)
            {
                return 0;
            }
            return addrs[index];
        }

        /// <summary>
        /// 是否有电池 前称重结果
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        ushort ConvertToAddr1(int index)
        {
            var addrs = new List<ushort>();
            addrs.Add(30409);
            addrs.Add(30410);
            addrs.Add(30411);
            addrs.Add(30412);
            addrs.Add(30413);
            addrs.Add(30414);
            addrs.Add(30415);
            addrs.Add(30416);
            addrs.Add(30417);
            addrs.Add(30418);
            addrs.Add(30419);
            addrs.Add(30420);
            addrs.Add(30421);
            addrs.Add(30422);
            addrs.Add(30423);
            addrs.Add(30424);
            addrs.Add(30425);
            addrs.Add(30426);
            addrs.Add(30427);
            addrs.Add(30428);
            addrs.Add(30429);
            addrs.Add(30430);
            addrs.Add(30431);
            addrs.Add(30432);
            addrs.Add(30433);
            addrs.Add(30434);
            addrs.Add(30435);
            addrs.Add(30436);
            addrs.Add(30437);
            addrs.Add(30438);
            addrs.Add(30439);
            addrs.Add(30440);

            if (index > addrs.Count - 1)
            {
                return 0;
            }
            return addrs[index];
        }
        /// <summary>
        /// 补液值地址
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        ushort ConvertToAddr2(int index)
        {
            var addrs = new List<ushort>();
            addrs.Add(30473);
            addrs.Add(30474);
            addrs.Add(30475);
            addrs.Add(30476);
            addrs.Add(30477);
            addrs.Add(30478);
            addrs.Add(30479);
            addrs.Add(30480);
            addrs.Add(30481);
            addrs.Add(30482);
            addrs.Add(30483);
            addrs.Add(30484);
            addrs.Add(30485);
            addrs.Add(30486);
            addrs.Add(30487);
            addrs.Add(30488);
            addrs.Add(30489);
            addrs.Add(30490);
            addrs.Add(30491);
            addrs.Add(30492);
            addrs.Add(30493);
            addrs.Add(30494);
            addrs.Add(30495);
            addrs.Add(30496);
            addrs.Add(30497);
            addrs.Add(30498);
            addrs.Add(30499);
            addrs.Add(30500);
            addrs.Add(30501);
            addrs.Add(30502);
            addrs.Add(30503);
            addrs.Add(30504);

            if (index > addrs.Count - 1)
            {
                return 0;
            }
            return addrs[index];
        }

        public void Start()
        {
            log.Info("start");
            timer.Enabled = true;
        }
        public void Stop()
        {
            log.Info("Stop");
            timer.Enabled = false;
        }


        #endregion 
    }
}
